/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import javax.microedition.lcdui.Graphics;

/**
 *
 * @author tho.hoang
 */
public class RadioBoxItem extends AFormItem {

    private String group;
    private boolean checked = false;
    private int paddingLeft;

    public RadioBoxItem(String id, String title) {
        super(id, title);
        this.checked = false;
        this.group = "";
    }

    public RadioBoxItem(String id, String title, String group) {
        super(id, title);
        this.checked = false;
        this.group = group;
    }

    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

    public void paint(int x, int y, Graphics g) {
        paint(x, y, g, false);
    }

    public void paint(int x, int y, Graphics g, boolean validate) {
        if (validate && !isPaint(x, y)) {
            return;
        }
        // pre-calc
        calculateHeight();
        setTitleRows(title);

        // paint background
        if (focus) {
            g.setColor(style.getSelectedBgColor());
            g.fillRect(x, y, width, height);
        }

        int tempX, tempY;

        // paint symbol
        tempX = x + style.getPadding().getLeft() + titleStyle.getPadding().getLeft();
        tempY = y + style.getPadding().getTop() + titleStyle.getPadding().getTop();
        paintUncheckSymbol(tempX, tempY, g);
        if (checked) {
            paintCheckedSymbol(tempX, tempY, g);
        }

        // paint text
        tempX = x + style.getPadding().getLeft() + paddingLeft + titleStyle.getPadding().getLeft();
        for (int i = 0; i < titleRows.length; i++) {
            font.getFontNormal().drawString(g, titleRows[i], tempX, tempY + i * (font.getFontNormal().getHeight() + titleStyle.getRowHeight()), Graphics.TOP | Graphics.LEFT);
        }
    }

    protected void calculateHeight() {
        int textH = 0;

        if (titleRows == null) {
            setTitleRows(title);
        }

        textH += titleRows.length * font.getFontNormal().getHeight();
        textH += (titleRows.length - 1) * titleStyle.getRowHeight();
        textH += style.getPadding().getTopAndBottom() + titleStyle.getPadding().getTopAndBottom();

        style.setHeight(textH);
        this.height = textH;
    }

    protected void setTitleRows(String title) {
        paddingLeft = font.getFontNormal().getHeight() + 5;
        titleStyle.setWidth(width - paddingLeft - style.getPadding().getLeftAndRight());

        if (title.length() > 0) {
//            titleRows = normalFont.splitWith(title, titleStyle.getWidth() - titleStyle.getPadding().getLeftAndRight());
            titleRows = new String[]{title};
        } else {
            titleRows = new String[]{""};
        }
    }

    private void paintCheckedSymbol(int x, int y, Graphics g) {
        int itemX = x + 3, itemY = y + 5, h = 0;
        g.setColor(0x393c39);
//        if (font.getFontSize() == Font2.SIZE10N) {
//            h = 7;
//            g.fillRect(itemX, itemY, h, 3);
//            g.fillRect(itemX + 2, itemY - 2, 3, h);
//            g.fillRect(itemX + 1, itemY - 1, h - 2, h - 2);
//        } else if (normalFont.getFontSize() == Font2.SIZE12N) {
//            h = 9;
//            g.fillRect(itemX, itemY, h, 5);
//            g.fillRect(itemX + 2, itemY - 2, 5, h);
//            g.fillRect(itemX + 1, itemY - 1, h - 2, h - 2);
//        } else if (normalFont.getFontSize() == Font2.SIZE14N) {
//            h = 11;
//            g.fillRect(itemX, itemY, h, 7);
//            g.fillRect(itemX + 2, itemY - 2, 7, h);
//            g.fillRect(itemX + 1, itemY - 1, h - 2, h - 2);
//        }

        h = 9;
        g.fillRect(itemX, itemY, h, 5);
        g.fillRect(itemX + 2, itemY - 2, 5, h);
        g.fillRect(itemX + 1, itemY - 1, h - 2, h - 2);
    }

    private void paintUncheckSymbol(int x, int y, Graphics g) {
        g.setColor(0x4a4d4a);
//        if (normalFont.getFontSize() == Font2.SIZE10N) {
//            g.drawArc(x, y, 12, 12, 0, 360);
//        } else if (normalFont.getFontSize() == Font2.SIZE12N) {
//            g.drawArc(x, y, 14, 14, 0, 360);
//        } else if (normalFont.getFontSize() == Font2.SIZE14N) {
//            g.drawArc(x, y, 16, 16, 0, 360);
//        }
        g.drawArc(x, y, 14, 14, 0, 360);
    }
}
